﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NMLX_Data_Management
{
    public partial class ProjectDetail : Form
    {
        private int RowIndex;
        private Form Project;
        private string ProName;



        public ProjectDetail()
        {
            InitializeComponent();
        }

        public ProjectDetail(Form1 form1, int RowIndex, string proname)
        {
            InitializeComponent();
            this.RowIndex = RowIndex;
            this.Project = form1;
            this.ProName = proname;
        }

        private void ProjectDetail_Load(object sender, EventArgs e)
        {
            // TODO: 这行代码将数据加载到表“nMLX_Data_MDataSet.ProjectDetail”中。您可以根据需要移动或删除它。
            //this.projectDetailTableAdapter.Fill(this.nMLX_Data_MDataSet.ProjectDetail);
            DetailQuery();
            dataGridView1.Columns[3].DefaultCellStyle.ForeColor = Color.Red;
            dataGridView1.Columns[0].Visible = false;
            dataGridView1.Columns[2].Visible = false;
        }

        private void DetailQuery()
        {
            DataSet ds = new DataSet();
            string sql = "select * from ProjectDetail where projectid='" + RowIndex + "'";
            ds = NMLX.DBUtility.DbHelperOleDb.Query(sql);
            this.dataGridView1.DataSource = ds.Tables[0];
            this.dataGridView1.Refresh();
            //this.dataGridView1.RowsDefaultCellStyle.BackColor = Color.Gray;
            this.dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;

        }

        private void ProjectDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            Project.Show();
            this.Hide();

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int projectid = 0;
            int Programid = 0;
            string projectname = "";
            string Programname = "";
            string path = "";
            string qcr = "";
            //点击了标题
            if (e.RowIndex==-1)
            {
                return;
            }
            //项目
            projectid = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
            projectname = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[1].Value);
            //程序
            Programid = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[2].Value);
            Programname = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells[3].Value);
            path = dataGridView1.Rows[e.RowIndex].Cells["path"].Value.ToString();
            //签出人
            qcr = dataGridView1.Rows[e.RowIndex].Cells["qcr"].Value.ToString();
            //点击了签出按钮
            if (e.ColumnIndex == 10)
            {

                //Fqianc fqianc = new Fqianc(this, projectid, Programid);
                Fqianc fqianc = new Fqianc(this, projectid, Programid, path, projectname, Programname, qcr);
                this.Hide();
                if (fqianc.ShowDialog() == DialogResult.OK)
                {
                    this.Show();
                    DetailQuery();
                }
                //this.Hide();
                //MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
            }
            //签入
            else if (e.ColumnIndex == 9)
            {

                Fqianr fqianr = new Fqianr(this, projectid, Programid, path, projectname, Programname, qcr);
                this.Hide();

                if (fqianr.ShowDialog() == DialogResult.OK)
                {
                    this.Show();
                    DetailQuery();
                }
            }
            else if (e.ColumnIndex == 12)
            {
                try
                {

                    DetailRecord detailRecord = new DetailRecord(projectid, Programid);
                    detailRecord.ShowDialog();
                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message);
                }

            }
            else if (e.ColumnIndex == 11)
            {
                //撤销签出
                try
                {
                    string sql = string.Format("update ProjectDetail set QCR='',QCDate='' where ProjectID='{0}' " +
                    "and ProjectDetail={1}", projectid, Programid);
                    string sql1 = string.Format("insert into DetailRecord (username,datet,ProgramName,qcqr,ProjectId,ProgramId) " +
                   "values ('{0}','{1}','{2}','{3}','{4}','{5}') ", qcr, DateTime.Now, "", "撤销签出", projectid, Programid);
                    
                    if (string.IsNullOrEmpty(qcr))
                    {
                        return;
                    }
                    //string str = Interaction.InputBox("确定要撤销签出吗？", "提示", "", -1, -1);
                    //if (string.IsNullOrEmpty(str))
                    //{
                    //    return;
                    //}
                    DialogResult dialogResult = MessageBox.Show(this, "确定要撤销签出吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                    if (dialogResult == DialogResult.OK)
                    {
                        if (NMLX.DBUtility.DbHelperOleDb.ExecuteSql(sql) > 0 && NMLX.DBUtility.DbHelperOleDb.ExecuteSql(sql1) > 0)
                        {
                            MessageBox.Show(this, "撤销成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            DetailQuery();
                        }
                        else
                        {
                            MessageBox.Show(this, "撤销失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

                        }

                    }
                    else
                    {
                        DetailQuery();
                    }


                }
                catch (Exception ex)
                {

                    MessageBox.Show(this, ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }


            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 添加程序ToolStripMenuItem_Click(object sender, EventArgs e)
        {

            AddProgram addProgram = new AddProgram(this.RowIndex, this.ProName);

            if (addProgram.ShowDialog() == DialogResult.OK)
            {
                DetailQuery();
            }
        }

        private void 查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DetailQuery();
        }

        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
            
        }
    }
}
